package com.friendfindertwo.control;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import com.example.friendfindertwo.R;
import model.Event;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class EventListAdapter extends BaseAdapter{
	private List<Event> mEventList;

	private Context cxt;
	private int selectedPosition;
	
	public EventListAdapter(Context cxt)
	{
//		Dummies dummies = new Dummies();
//		this.mEventList=dummies.getEventList();
		this.cxt=cxt;
		this.selectedPosition =-1;
	}
	
	public EventListAdapter(List<Event> eventList, Context context){
//		Dummies dummies = new Dummies();
//		this.mEventList=dummies.getEventList();

		this.mEventList = eventList;
		//Log.i("EventListAdapter",this.mEventList.toString());
		this.cxt=context;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.mEventList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mEventList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Log.i("EventListAdapter.getView", "startfunction");
		View v;
		if(convertView==null)
		{
			v=View.inflate(cxt, R.layout.event_list_single_item, null);
			Log.i("EventListAdapter.getView - if inflate statement", "startfunction");

		}else{
			v=convertView;
			Log.i("EventListAdapter.getView - else inflate statement", "startfunction");
		}
		
		Log.i("EventListAdapter.getView - setText statement", "end");
		v=fillDataEvent(v,position);
//		ViewGroup parentLayout = (ViewGroup)v.findViewById(R.id.Parent);
//		if(selectedPosition == position){
//			parentLayout.setBackgroundResource(R.color.blue);
//		} else {
//			parentLayout.setBackgroundResource(R.color.none);
//		}
		
		return v;
	}
	
	@SuppressWarnings("deprecation")
	public View fillDataEvent(View v, int itemNumber)
	{
		if(((Event)getItem(itemNumber)).getCategorie().equals("Food")) {
			((ImageView) v.findViewById(R.id.imageViewCategory)).setImageResource(R.drawable.icon_food);
		} else if(((Event)getItem(itemNumber)).getCategorie().equals("Party")) {
			((ImageView) v.findViewById(R.id.imageViewCategory)).setImageResource(R.drawable.icon_party);
		} else {
			((ImageView) v.findViewById(R.id.imageViewCategory)).setImageResource(R.drawable.icon_adventure);
		}
		
		((TextView)v.findViewById(R.id.textViewName)).setText(((Event)getItem(itemNumber)).getCreator().getAccountName());
		((TextView)v.findViewById(R.id.textViewName)).setText(((Event)getItem(itemNumber)).getCreator().getAccountName());
		((TextView)v.findViewById(R.id.textViewTitle)).setText(((Event)getItem(itemNumber)).getTitle());
		Date date = ((Event) getItem(itemNumber)).getStartDate();
		((TextView)v.findViewById(R.id.textViewDate)).setText(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(date));
		//((TextView)v.findViewById(R.id.textViewDay)).setText(((Event)getItem(itemNumber)).getStartDate().getDate());
		return v;
	}
	public void setSelectedItem(int position){
		this.selectedPosition = position;
		notifyDataSetChanged();
	}

	private class ViewHolder{
		
	}

	
}
